| Conditions | 1 |
| Paths | 16 |
| Total Lines | 150 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 27 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* |
||
| 9 | function ($interval, $timeout, $window) { |
||
| 10 | 'use strict'; |
||
| 11 | |||
| 12 | var signaturePad, element, EMPTY_IMAGE = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjgAAADcCAQAAADXNhPAAAACIklEQVR42u3UIQEAAAzDsM+/6UsYG0okFDQHMBIJAMMBDAfAcADDATAcwHAAwwEwHMBwAAwHMBzAcAAMBzAcAMMBDAcwHADDAQwHwHAAwwEMB8BwAMMBMBzAcADDATAcwHAADAcwHADDAQwHMBwAwwEMB8BwAMMBDAfAcADDATAcwHAAwwEwHMBwAAwHMBzAcAAMBzAcAMMBDAcwHADDAQwHwHAAwwEwHMBwAMMBMBzAcAAMBzAcwHAADAcwHADDAQwHMBwAwwEMB8BwAMMBDAfAcADDATAcwHAAwwEwHMBwAAwHMBzAcCQADAcwHADDAQwHwHAAwwEMB8BwAMMBMBzAcADDATAcwHAADAcwHMBwAAwHMBwAwwEMBzAcAMMBDAfAcADDAQwHwHAAwwEwHMBwAAwHMBzAcAAMBzAcAMMBDAcwHADDAQwHwHAAwwEMB8BwAMMBMBzAcADDATAcwHAADAcwHMBwAAwHMBwAwwEMB8BwAMMBDAfAcADDATAcwHAAwwEwHMBwAAwHMBzAcAAMBzAcAMMBDAcwHADDAQwHwHAAwwEMB8BwAMMBMBzAcADDkQAwHMBwAAwHMBwAwwEMBzAcAMMBDAfAcADDAQwHwHAAwwEwHMBwAMMBMBzAcAAMBzAcwHAADAcwHADDAQwHMBwAwwEMB8BwAMMBMBzAcADDATAcwHAADAcwHMBwAAwHMBwAwwEMBzAcAMMBDAegeayZAN3dLgwnAAAAAElFTkSuQmCC'; |
||
| 13 | |||
| 14 | return { |
||
| 15 | restrict: 'EA', |
||
| 16 | replace: true, |
||
| 17 | template: '<div class="signature" style="width: 100%; max-width:{{width}}px; height: 100%; max-height:{{height}}px;"><canvas style="display: block; margin: 0 auto;" ng-mouseup="onMouseup()" ng-mousedown="notifyDrawing({ drawing: true })"></canvas></div>', |
||
| 18 | scope: { |
||
| 19 | accept: '=?', |
||
| 20 | clear: '=?', |
||
| 21 | disabled: '=?', |
||
| 22 | dataurl: '=?', |
||
| 23 | height: '@', |
||
| 24 | width: '@', |
||
| 25 | notifyDrawing: '&onDrawing', |
||
| 26 | }, |
||
| 27 | controller: [ |
||
| 28 | '$scope', |
||
| 29 | function ($scope) { |
||
| 30 | $scope.accept = function () { |
||
| 31 | |||
| 32 | return { |
||
| 33 | isEmpty: $scope.dataurl === EMPTY_IMAGE, |
||
| 34 | dataUrl: $scope.dataurl |
||
| 35 | }; |
||
| 36 | }; |
||
| 37 | |||
| 38 | $scope.onMouseup = function () { |
||
| 39 | $scope.updateModel(); |
||
| 40 | |||
| 41 | // notify that drawing has ended |
||
| 42 | $scope.notifyDrawing({ drawing: false }); |
||
| 43 | }; |
||
| 44 | |||
| 45 | $scope.updateModel = function () { |
||
| 46 | /* |
||
| 47 | defer handling mouseup event until $scope.signaturePad handles |
||
| 48 | first the same event |
||
| 49 | */ |
||
| 50 | $timeout().then(function () { |
||
| 51 | $scope.dataurl = $scope.signaturePad.isEmpty() ? EMPTY_IMAGE : $scope.signaturePad.toDataURL(); |
||
| 52 | }); |
||
| 53 | }; |
||
| 54 | |||
| 55 | $scope.clear = function () { |
||
| 56 | $scope.signaturePad.clear(); |
||
| 57 | $scope.dataurl = EMPTY_IMAGE; |
||
| 58 | }; |
||
| 59 | |||
| 60 | $scope.$watch("dataurl", function (dataUrl) { |
||
| 61 | if (!dataUrl || $scope.signaturePad.toDataURL() === dataUrl) { |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | $scope.setDataUrl(dataUrl); |
||
| 66 | }); |
||
| 67 | } |
||
| 68 | ], |
||
| 69 | link: function (scope, element, attrs) { |
||
| 70 | var canvas = element.find('canvas')[0]; |
||
| 71 | var parent = canvas.parentElement; |
||
| 72 | var scale = 0; |
||
| 73 | var ctx = canvas.getContext('2d'); |
||
| 74 | |||
| 75 | var width = parseInt(scope.width, 10); |
||
| 76 | var height = parseInt(scope.height, 10); |
||
| 77 | |||
| 78 | canvas.width = width; |
||
| 79 | canvas.height = height; |
||
| 80 | |||
| 81 | scope.signaturePad = new SignaturePad(canvas); |
||
| 82 | |||
| 83 | scope.setDataUrl = function(dataUrl) { |
||
| 84 | var ratio = Math.max(window.devicePixelRatio || 1, 1); |
||
| 85 | |||
| 86 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
||
| 87 | ctx.scale(ratio, ratio); |
||
| 88 | |||
| 89 | scope.signaturePad.clear(); |
||
| 90 | scope.signaturePad.fromDataURL(dataUrl); |
||
| 91 | |||
| 92 | $timeout().then(function() { |
||
| 93 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
||
| 94 | ctx.scale(1 / scale, 1 / scale); |
||
| 95 | }); |
||
| 96 | }; |
||
| 97 | |||
| 98 | scope.$watch('disabled', function (val) { |
||
| 99 | val ? scope.signaturePad.off() : scope.signaturePad.on(); |
||
| 100 | }); |
||
| 101 | |||
| 102 | var calculateScale = function() { |
||
| 103 | var scaleWidth = Math.min(parent.clientWidth / width, 1); |
||
| 104 | var scaleHeight = Math.min(parent.clientHeight / height, 1); |
||
| 105 | |||
| 106 | var newScale = Math.min(scaleWidth, scaleHeight); |
||
| 107 | |||
| 108 | if (newScale === scale) { |
||
| 109 | return; |
||
| 110 | } |
||
| 111 | |||
| 112 | var newWidth = width * newScale; |
||
| 113 | var newHeight = height * newScale; |
||
| 114 | canvas.style.height = Math.round(newHeight) + "px"; |
||
| 115 | canvas.style.width = Math.round(newWidth) + "px"; |
||
| 116 | |||
| 117 | scale = newScale; |
||
| 118 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
||
| 119 | ctx.scale(1 / scale, 1 / scale); |
||
| 120 | }; |
||
| 121 | |||
| 122 | var resizeIH = $interval(calculateScale, 200); |
||
| 123 | scope.$on('$destroy', function () { |
||
| 124 | $interval.cancel(resizeIH); |
||
| 125 | resizeIH = null; |
||
| 126 | }); |
||
| 127 | |||
| 128 | angular.element($window).bind('resize', calculateScale); |
||
| 129 | scope.$on('$destroy', function () { |
||
| 130 | angular.element($window).unbind('resize', calculateScale); |
||
| 131 | }); |
||
| 132 | |||
| 133 | calculateScale(); |
||
| 134 | |||
| 135 | element.on('touchstart', onTouchstart); |
||
| 136 | element.on('touchend', onTouchend); |
||
| 137 | |||
| 138 | function onTouchstart(event) { |
||
| 139 | scope.$apply(function () { |
||
| 140 | // notify that drawing has started |
||
| 141 | scope.notifyDrawing({ drawing: true }); |
||
| 142 | }); |
||
| 143 | event.preventDefault(); |
||
| 144 | } |
||
| 145 | |||
| 146 | function onTouchend(event) { |
||
| 147 | scope.$apply(function () { |
||
| 148 | // updateModel |
||
| 149 | scope.updateModel(); |
||
| 150 | |||
| 151 | // notify that drawing has ended |
||
| 152 | scope.notifyDrawing({ drawing: false }); |
||
| 153 | }); |
||
| 154 | event.preventDefault(); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | }; |
||
| 158 | } |
||
| 159 | ]); |
||
| 163 |